<?php

namespace App\Api\Repositories;

use App\Api\Services\ApibaiduFace;
use App\Api\Services\JsonService;

class ApiFace
{
    const SCORE = 50;
    const BASE64 ='BASE64';
    const URL ='URL';
    const FACE_TOKEN ='FACE_TOKEN';

    /**
     * 搜索
     * @param $data
     * @return mixed|string
     */
    public static function searchFace($data)
    {
        $access_token = ApibaiduFace::getBaiduAccessToken();
        $url = config('common.bdApiFace.searchFace').$access_token;
        $groupIdList = "jwei";//此处为用户组名
        if (!empty($data['group_id'])) {
            $groupIdList = $data['group_id'];
        }
        $body = [
            'image' => $data,
            'image_type' => self::BASE64,
            'group_id_list' => $groupIdList,
        ];

        return json_decode(ApibaiduFace::requestPost($url, json_encode($body)),true);
    }



    /**
     * 人脸添加
     * @param array $data
     * @param string $user_id
     * @return array
     */
    public static function saveFace($data)
    {
        $access_token = ApibaiduFace::getBaiduAccessToken();
        $url = config('common.bdApiFace.addFace').$access_token;
        $groupIdList = "jwei";//此处为用户组名
        if (!empty($data['group_id'])) {
            $groupIdList = $data['group_id'];
        }
        $user_id = rand(0,99999999);
        if (!empty($data['user_id'])) $user_id = time();

        $body = [
            'image' => $data['image'],
            'image_type' => self::BASE64,
            "group_id" => $groupIdList,
            "user_id" => $user_id,
        ];

        return json_decode(ApibaiduFace::requestPost($url, json_encode($body)),true);

    }

    /**
     * 人脸更新
     * @param array $data
     * @return array
     */
    public static function updateFace($data = [])
    {
        $access_token = ApibaiduFace::getBaiduAccessToken();
        $img = public_path(). '/uploads/face/jwei.jpg';
        $img = base64_encode(file_get_contents($img));//人脸图像地址
        $url = config('common.bdApiFace.updateFace').$access_token;
        $groupIdList = "jwei";//此处为用户组名
        if (!empty($data['group_id'])) {
            $groupIdList = $data['group_id'];
        }
        $body = [
            'image' => $img,
            'image_type' => self::BASE64,
            "group_id" => $groupIdList,
            "user_id" => "111",
        ];

        $res =json_decode(ApibaiduFace::requestPost($url, json_encode($body)),true);
        if ($res['error_code'] == 0) {
            return JsonService::success('修改成功',$res);
        } else {
            return JsonService::fail($res['error_msg']) ;
        }
    }

}
